/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common;

import org.telosys.util.StrUtil;

/**
 * Utility class for "symbolic variables" operations ( set of static methods )<br>
 * Designed to apply the values of the Telosys "symbolic variables" : <br>
 * ${SCREENNAME}, ${SCREENNAME_LC} or ${SCREENNAME_UC} 
 * 
 * @author Laurent GUERIN
 * @since 1.1.0
 *  
 */


public class SymbolicConfigVar
{
    //----------------------------------------------------------------------------------------------------    
    /**
     * Replaces the first occurrence of ${SCREENNAME}, ${SCREENNAME_LC} or ${SCREENNAME_UC}
     * @param sOriginal
     * @param sValue
     * @return
     */
    private static String replaceScreenNameVar(String sOriginal, String sValue) 
    {
        if ( sOriginal.indexOf( TelosysConfigVar.SCREENNAME ) >= 0 )
        {
            return StrUtil.replaceVar(sOriginal, TelosysConfigVar.SCREENNAME, sValue );
        }
        else if ( sOriginal.indexOf(TelosysConfigVar.SCREENNAME_UC) >= 0 )
        {
            return StrUtil.replaceVar(sOriginal, TelosysConfigVar.SCREENNAME_UC, sValue.toUpperCase() );
        }
        else if ( sOriginal.indexOf(TelosysConfigVar.SCREENNAME_LC) >= 0 )
        {
            return StrUtil.replaceVar(sOriginal, TelosysConfigVar.SCREENNAME_LC, sValue.toLowerCase() );
        }
        return sOriginal ;
    }
    
    //----------------------------------------------------------------------------------------------------
    private static boolean containsScreenNameVarToReplace(String s) 
    {
        if ( s.indexOf( TelosysConfigVar.SCREENNAME   ) >= 0 ) return true ;
        if ( s.indexOf( TelosysConfigVar.SCREENNAME_UC) >= 0 ) return true ;
        if ( s.indexOf( TelosysConfigVar.SCREENNAME_LC) >= 0 ) return true ;
        return false ;
    }
    
    //----------------------------------------------------------------------------------------------------
    /**
     * Applies the given "screen name value" to the given pattern <br>
     * Replaces all the occurrences of the symbolic variables <br>
     * "${SCREENNAME}", "${SCREENNAME_UC}" and "${SCREENNAME_LC}" in the given pattern
     * 
     * @param pattern the pattern containing 0 to N symbolic variables
     * @param screenNameValue the value to apply
     * @return
     */
    public static String applyScreenName( String pattern, String screenNameValue )
    {
        if ( pattern != null && screenNameValue != null )
        {
            //--- Replace all the symbolic variables ${xxx} in the pattern
            String s = pattern ;
            while ( containsScreenNameVarToReplace(s) )
            {
                s = replaceScreenNameVar( s, screenNameValue );
            }
            return s ;
        }
        return null ;
    }

    //-----------------------------------------------------------------------------
    /**
     * Applies the given "bean name value" to the given pattern <br>
     * The string "${BEANNAME}" in the pattern will be replaced by the bean name <br>
     * NB : The "pattern" is supposed to contain only one occurrence of "${BEANNAME}"
     * 
     * @param pattern the pattern ( e.g. "my.package.${BEANNAME}DAO" )
     * @param beanNameValue the value to apply ( e.g. "Book" )
     * @return the resulting string ( e.g. "my.package.BookDAO" )
     * @since 1.0.0
     */
    public static String applyBeanName( String pattern, String beanNameValue )
    {
        if ( pattern != null && beanNameValue != null )
        {
    	    //String sClassName = StrUtil.replaceVar(sPattern, "${BEANNAME}", sBeanName);
    	    String sClassName = StrUtil.replaceVar(pattern, TelosysConfigVar.BEANNAME, beanNameValue);
    	    return sClassName ;
        }
        else
        {
            throw new TelosysRuntimeException("null parameter");
        }
    }


}